#include <vector>
#include <string>
#include <boost/filesystem.hpp>
#include "log.hpp"
#include "index.hpp"
// #include "searcher.hpp"

const std::string Hpath = "asset/input";
const std::string purePath = "asset/pure/";

bool extractTitle(const std::string& content,std::string* title){
    std::string lableStart = "<title>",lableEnd = "</title>";
    int start = content.find(lableStart);
    int end = content.find(lableEnd);
    if(start==std::string::npos || end==std::string::npos){
        return false;
    }
    //<title>ovo</title>
    start += lableStart.size();
    if(start > end){
        return false;
    }
    *title = content.substr(start,end-start);
    return true;
}

void discardLable(const std::string& content,std::string* body){
    typedef enum Status{
        LABLE,
        CONTENT
    }Status;
    Status sta = LABLE;
    for(auto c:content){
        switch (sta)
        {
        case LABLE:
            if(c=='>'){
                sta = CONTENT;
            }
            break;
        case CONTENT:
            if(c=='<'){
                sta = LABLE;
                break;
            }
            if(c=='\n'){
                c=' ';
            }
            *body+=c;
        }
    }
}

void extractUrl(const std::string& filePath,std::string* url)
{
    static std::string official = "https://www.boost.org/doc/libs/latest/doc/html"; 
    *url = official+ filePath.substr(Hpath.size());
}

bool parseHtml(const std::vector<std::string>& files,std::vector<Doc>* docs)
{
    int rawSize = 0;
    for(const auto& file:files){
        Doc doc;
        std::string content;
        Util::ReadFile(file,&content);
        rawSize += content.size();
        if(!extractTitle(content,&doc._title)){
            continue;
        }
        discardLable(content,&doc._body);
        extractUrl(file,&doc._url);
        // docs->emplace_back(doc);
        //移动构造避免拷贝
        docs->push_back(std::move(doc));
    }
    LOG(logLevel::INFO)<<"总html文件占用"<<rawSize<<"个字节";
    return true;
}

bool WritePure(const std::vector<Doc> docs,const std::string& pureFile){
    std::string src;
#define SEP "\3"
#define DEL "\n"
    for(const auto& d:docs)
    {
        src += d._title + SEP + d._body + SEP + d._url + DEL;
    }
    LOG(logLevel::INFO)<<"清洗标签后的数据占用"<<src.size()<<"个字节";
    return Util::WriteFile(src,pureFile);
}

bool searchHtml(const std::string& src,std::vector<std::string>* fileList)
{
    namespace fs = boost::filesystem;
    fs::path dir(src);
    if(!fs::exists(dir) || !fs::is_directory(dir)){
        LOG(logLevel::ERROR)<< "File error";
        return false;
    }

    for (const auto& entry : fs::recursive_directory_iterator(dir)) {
        if (fs::is_regular_file(entry)) {
            const fs::path& file_path = entry.path();
            if (file_path.extension() == ".html") {
                fileList->push_back(file_path.string());
            }
        }
    }
    return true;

}

int main()
{
<<<<<<< HEAD:source/pretreat.cpp
    // std::string words = "I love walking with you on the street";
    // std::vector<std::string> v;
    // JiebaUtil::CutString(words,&v);
    // for(auto& e : v)  
    // {
    //     std::cout<<e<<std::endl;
    // }
//     std::string s = "heloo";
//     auto iter1 = s.begin();
//     auto iter2 = s.end();
//     std::cout<<iter2-iter1<<std::endl;
//     if(iter2 - 10 < iter1){
//         std::cout<<"Yesoo"<<std::endl;
//     }
    std::vector<std::string> fileList;
    if(!searchHtml(Hpath,&fileList)){
        return 1;
    }
    LOG(logLevel::INFO)<<"共检索到"<<fileList.size()<<"个文件";
    std::vector<Doc> docs;
    parseHtml(fileList,&docs); 
    if(!WritePure(docs,purePath+"pure.txt")){
        std::cout<<"WritePure fail"<<std::endl;
    }
    // std::string s = "jHUiolHL:LEHLHo";
    // boost::to_lower(s);
    // std::cout<<s<<std::endl;
    // std::vector<int> a = {1,2,3};
    // std::vector<int> b = {1,2,3};
    // a.insert(a.end(),b.begin(),b.end());
    // for(auto i : a){
    //     std::cout<<i<<std::endl;
    // }
=======
    std::string words = "I love walking with you on the street";
    std::vector<std::string> v;
    JiebaUtil::CutString(words,&v);
    for(auto& e : v)  
    {
        std::cout<<e<<std::endl;
    }
//     std::string s = "heloo";
//     auto iter1 = s.begin();
//     auto iter2 = s.end();
//     std::cout<<iter2-iter1<<std::endl;
//     if(iter2 - 10 < iter1){
//         std::cout<<"Yesoo"<<std::endl;
//     }
    // std::vector<std::string> fileList;
    // if(!searchHtml(Hpath,&fileList)){
    //     return 1;
    // }
    // std::vector<Doc> docs;
    // parseHtml(fileList,&docs); 
    // if(!WritePure(docs,purePath+"pure.txt")){
    //     std::cout<<"WritePure fail"<<std::endl;
    // }
    // std::string s = "jHUiolHL:LEHLHo";
    // boost::to_lower(s);
    // std::cout<<s<<std::endl;
    // std::vector<int> a = {1,2,3};
    // std::vector<int> b = {1,2,3};
    // a.insert(a.end(),b.begin(),b.end());
    // for(auto i : a){
    //     std::cout<<i<<std::endl;
    // }
>>>>>>> 080e019c2dfe1ffe0ec9c75ee02120374651660b:test.cpp

    // Json::Value root;
    // Json::Value test1;
    // Json::Value test2;
    // test1["thirsty"] = "drink";
    // test1["hungry"]  = "eat";
    // test1["tired"]  = "sleep";
    // test2["hungry"] = "eat";
    // test2["uuoo"] = "happy";
    // // Json::StyledWriter writer;
    // Json::FastWriter writer;
    // root.append(test1);
    // root.append(test2);
    // std::string s = writer.write(root);
    // std::cout<<s<<std::endl;

    return 0;
}
